home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / MS Works Merge Enhancer / TCstring.cp < prev    next >
Encoding:
Text File  |  1991-06-20  |  1.7 KB  |  114 lines  |  [TEXT/MPS ]

  1. #include <TString.h>
  2. #include <memory.h>
  3. #include <packages.h>
  4. #include <strings.h>
  5.  
  6. void *operator new (size_t size)
  7. {
  8.     return (void *)NewPtr(size);
  9. }
  10.  
  11. void operator delete (void * thePtr)
  12. {
  13.     if (thePtr) DisposPtr((Ptr) thePtr);
  14. }
  15.  
  16. CString::CString (char *s)
  17. {
  18.     len=strlen(s);
  19.     str=new char[len+1];
  20.     strcpy(str, s);
  21. }
  22.  
  23. CString::CString (CString& s)
  24. {
  25.     len=s.len;
  26.     str=new char[len+1];
  27.     strcpy(str, s.str);
  28. }
  29.  
  30. CString::CString (CString *s)
  31. {
  32.     len=s->len;
  33.     str=new char[len+1];
  34.     strcpy(str, s->str);
  35. }
  36.  
  37. CString::CString (long theNum)
  38. {
  39.     char *p = new char[32];
  40.     numtostring(theNum, p);
  41.     len=strlen(p);
  42.     str= new char[len];
  43.     strcpy(str, p);
  44.     delete p;
  45. }
  46.  
  47. CString::CString (char *s, char *s1)
  48. {
  49.     len=strlen(s)+strlen(s1);
  50.     str=new char[len+1];
  51.     strcpy(str, s);
  52.     strcat(str, s1);
  53. }
  54.  
  55. CString::CString (const char *s, short theLen)
  56. {
  57.     len=theLen;
  58.     str=new char[len+1];
  59.     strncpy(str, s, len);
  60.     str[len]=0;
  61. }
  62.  
  63. CString::~CString ()
  64. {
  65.     delete str;
  66. }
  67.  
  68. CString operator +(const CString& s1, const CString& s2)
  69. {
  70.     return CString(s1.str, s2.str);
  71. }
  72.  
  73. void CString::operator +=(const CString& s1)
  74. {
  75.     char *oldstr=str;
  76.     
  77.     len=strlen(str)+strlen(s1.str);
  78.     str=new char[len+1];
  79.     strcpy(str, oldstr);
  80.     strcat(str, s1.str);
  81.     delete oldstr;
  82. }
  83.  
  84. CString& CString::operator =( const CString& s1)
  85. {
  86.     delete str;
  87.     
  88.     len=strlen(s1.str);
  89.     str=new char[len+1];
  90.     strcpy(str, s1.str);
  91.     return *this;
  92. }
  93.  
  94. CString::operator char* () {
  95.     char *p = new char[len+1];
  96.     strcpy(p, str);
  97.     return p;
  98. }
  99.  
  100. CString::operator StringPtr () {
  101.     char *p = new char[len+1];
  102.     strcpy((char *)p, str);
  103.     c2pstr(p);
  104.     return (StringPtr)p;
  105. }
  106.  
  107. CString::operator Handle () {
  108.     Handle p = NewHandle((Size)len+1);
  109.     HLock(p);
  110.     strcpy(*p, str);
  111.     HUnlock(p);
  112.     return p;
  113. }
  114.